package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.constant.ExamineSellerNum;
import com.dongdongshop.mapper.TbSellerMapper;
import com.dongdongshop.pojo.seller.TbSeller;
import com.dongdongshop.pojo.seller.TbSellerExample;
import com.dongdongshop.service.seller.SellerService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Eayon
 * @Date 2020/2/27 10:13
 */
@Service
@Component
public class SellerServiceImpl implements SellerService {
    @Resource
    private TbSellerMapper sellerMapper;

    /**
     * 注册商家
     * @param seller
     */
    @Override
    public void registerSeller(TbSeller seller) {
        sellerMapper.insertSelective(seller);
    }

    /**
     * 查询所有待审核的商家信息(状态为0)
     * @return sellers
     */
    @Override
    public List<TbSeller> getNotExamineSellerInfo() {
        TbSellerExample example = new TbSellerExample();
        TbSellerExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo("0");
        List<TbSeller> sellers = sellerMapper.selectByExample(example);
        return sellers;
    }

    /**
     * 根据公司名称或者店铺名称模糊查询未审核的商家信息
     * @param name
     * @param nickName
     * @return sellers
     */
    @Override
    public List<TbSeller> getLikeNotExamineSeller(String name, String nickName) {
        List<TbSeller> sellers = sellerMapper.getLikeNotExamineSeller(name,nickName);
        return sellers;
    }

    /**
     * 根据主键id查询商家信息
     * @param id
     * @return
     */
    @Override
    public TbSeller findSellerById(String id) {
        TbSeller seller = sellerMapper.selectByPrimaryKey(id);
        return seller;
    }

    /**
     * 商铺审核  flag参数三种状态 1=审核通过 2=审核不通过 3=关闭商铺
     * @param seller,flag
     * @param flag
     */
    @Override
    public void updateSellerStatusById(TbSeller seller, String flag) {
        //如果flag=3代表删除店铺
        if(ExamineSellerNum.CLOSE.getCode().equals(flag)){
            sellerMapper.deleteByPrimaryKey(seller.getSellerId());
            return;
        }
        //flage不等于三就做其他审核操作
        seller.setStatus(flag);
        sellerMapper.updateByPrimaryKeySelective(seller);
    }

    /**
     * 根据id查找商家信息
     * @param username
     * @return
     */
    @Override
    public TbSeller getSellerById(String username) {
        TbSeller seller = sellerMapper.selectByPrimaryKey(username);
        return seller;
    }
}
